package com.zyh.json;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * @Author zyh
 * @Date 2024/8/13 15:43
 * @Description 读取json文件的字符串
 */

public class JSONDemo4 {

    public static void main(String[] args) throws IOException {
        //创建流读取json文件
        BufferedReader br = new BufferedReader(new FileReader("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day15-code\\src\\com\\zyh\\json\\json2.json"));
        String line=null;
        StringBuilder sb = new StringBuilder();
        while ((line=br.readLine())!=null){
            //调用trim方法将每一行的空格去掉，使用sb来循环拼接每一行内容
            sb.append(line.trim());
        }
        //循环拼接完成后将sb转为字符串
        String s = sb.toString();
        //将字符串转为数组
        List<User> list = JSON.parseArray(s, User.class);
        System.out.println("list = " + list);
    }
    public static void main1(String[] args) throws IOException {
        //创建流读取json1.json文件内容
        BufferedReader br = new BufferedReader(new FileReader("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day15-code\\src\\com\\zyh\\json\\json1.json"));
        String line=null;
        //循环读取每一行内容
        while ((line=br.readLine())!=null){
            User user = JSON.parseObject(line, User.class);
            System.out.println("user = " + user);
        }
    }
}
